public class No016 {
    /**
     * 给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。
     * 保证base和exponent不同时为0
     */
    public static void main(String[] args) {
        System.out.println(Power(0, 0));
    }

    public static double Power(double base, int exponent) {
        if (base == 0.0 && exponent < 0) {
            return 0.0;
        }
        if (exponent < 0) {
            exponent = -exponent;
            double result = PowerCore(base, exponent);
            return 1.0 / result;
        }
        return PowerCore(base, exponent);
    }

    // pow(a,n) = pow(a,n/2)*pow(a,n/2)     n为偶数
    //            pow(a,n/2)*pow(a,n/2)*a   n为奇数
    public static double PowerCore(double base, int exponent) {
        if (exponent == 0) return 1;
        if (exponent == 1) return base;
        double result = PowerCore(base, exponent >> 1);
        result *= result;
        if ((exponent & 1) == 1) {
            result *= base;
        }
        return result;
    }

}
